<?php

/**
 * 房源
 */

namespace app\api\controller;

use app\api\model\HotCity;
use app\api\model\HouseVillage;
use app\api\model\House as HouseModel;
use app\api\model\IntervalConfig;
use app\api\model\Region;
use app\common\controller\ApiController;
use app\common\lib\RespCode;
use map\BaiDu;
use think\Db;
use think\Exception;
//use think\Request;
use think\facade\Request;

class House extends ApiController {

//    protected $notNeedToken = ['interval', 'houselist', 'villagelist', 'getvillageinfo', 'cityhome', 'housemap', 'housemaplist', 'gethouseinfo', 'housemaplist2', 'getrepeathouse'];

    protected $notNeedPhone = ['interval', 'houselist', 'villagelist', 'getvillageinfo', 'cityhome', 'housemap', 'housemaplist', 'gethouseinfo', 'housemaplist2', 'getrepeathouse'];

    protected $isArrayResult = ['housemap'];

    /**
     * 房源列表条件数据
     */
    public function interval() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $city_code = $params['city_code'];
            $discount_interval = IntervalConfig::all_intervel($city_code)['discount_interval'];//折扣区间
            $price_interval = IntervalConfig::all_intervel($city_code)['price_interval'];//价格区间
            $build_area_interval = IntervalConfig::all_intervel($city_code)['build_area_interval'];//面积区间
            $Region = Region::get_list($city_code);//区域，商圈
            $a_status = IntervalConfig::$a_status; //拍卖状态
            $a_type = IntervalConfig::$a_type; //拍卖阶段
            $bedroom = IntervalConfig::$bedroom;//户型
            $orderby = IntervalConfig::$orderby;//排序
            return $this->resData('success', compact('discount_interval', 'price_interval', 'build_area_interval', 'house_age', 'house_floor', 'Region', 'a_status', 'a_type', 'bedroom', 'orderby', 'decoration', 'elevator'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源列表
     * @return false|string
     */
    public function houseList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $HouseModel = new HouseModel();
            $re = $HouseModel->get_house_data($params);
            return $this->resData('success', ['pageNum' => $re[3], 'page' => $re[2], 'count' => $re[1], 'data' => $re[0]]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源详情
     * @return false|string
     */
    public function getHouseInfo() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) {
                return $this->resErr();
            }
            if (empty(Db::name('house')->getByid($params['id']))){
                throw new Exception('数据不存在', RespCode::DATA_ERR);
            }
            $HouseModel = new HouseModel();
            $data =$HouseModel->get_house_detail($params, $this->headers);
            return $this->resData('success', ['house_info' => $data[0], 'history' => $data[1], 'success_history' => $data[2],'nearData'=>$data[3]]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 小区列表
     * @return false|string
     */
    public function villageList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $house_village = new HouseVillage();
            empty($params['long']) ? $long = 10 : $long = intval($params['long']);
            empty($params['start']) ? $start = 1 : $start = intval($params['start']);
            $house_village->get_list($start, $long, $params, $page, $pageNum, $data, $count);
            return $this->resData('success', ['pageNum' => $pageNum, 'page' => $page, 'count' => $count, 'data' => $data]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 小区房源详情
     * @return false|string
     */
    public function getVillageInfo() {
        try {
            $params = $this->checkParams('village_id');
            if ($params === false) {
                return $this->resErr();
            }
            if (empty(Db::name('house_village')->getByid($params['village_id'])))
                throw new Exception('数据不存在', RespCode::DATA_ERR);
            $info = HouseVillage::get_detail($params['village_id'], $this->headers);//详情
            $data2 = HouseVillage::get_village_house($params);//小区房源
            return $this->resData('success', ['info' => $info, 'data' => $data2[0], 'count' => $data2[1], 'page' => $data2[2], 'pageNum' => $data2[3]]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 首页城市
     * @return false|string
     */
    public function CityHome() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $Region = new Region();
            $info=$this->uInfo;
            $data = $Region->getCityHome($info);
//            $hot_city = HotCity::getList();
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 地图找房-区域
     * @return false|string
     */
    public function houseMap() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $Region = new Region();
            $data = $Region->get_area($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 地图找房-房源列表-经纬度
     * @return false|string
     */
    public function houseMapList() {
        try {
            $params = $this->checkParams('lat,lng');
            if ($params === false) {
                return $this->resErr();
            }
            $HouseModel = new HouseModel();
            $HouseModel->houseMapList($params, $data, $count);
            return $this->resData('success', compact('count', 'data'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 地图找房-房源列表-条件
     * @return false|string
     */
    public function houseMapList2() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $HouseModel = new HouseModel();
            $HouseModel->houseMapList2($params, $data, $count);
            return $this->resData('success', compact('count', 'data'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 智能选房
     * @return false|string
     */
    public function autoHouse() {
        try {
            $params = $this->checkParams('start,long');
            if ($params === false) {
                return $this->resErr();
            }
            $HouseModel = new HouseModel();
            $data = $HouseModel->autoHouse($params);
            $start1 = (int)$params['start'];
            $long = $params['long'];
            $start = ($start1 - 1) * $long;
            $data1 = array_slice($data, $start, $long);
            $count = count($data);
            if ($long > 0) {
                $pageNum = ceil($count / $long);
            } else {
                $pageNum = 1;
            }
            return $this->resData('success', ['count' => count($data), 'pageNum' => $pageNum, 'page' => empty($count) ? 0 : $start1, 'data' => $data1]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 智能房源推送房源
     * @return false|string
     */
    public function getRecommend() {
        try {
            $params = $this->checkParams('message_id');
            if ($params === false) {
                return $this->resErr();
            }
            if (empty(db("message_agent_house")->where("agent_id", $params['agent_id'])->where("id", $params['message_id'])->find()))
                throw new Exception('数据不存在', RespCode::DATA_ERR);
            $HouseModel = new HouseModel();
            $re = $HouseModel->get_house_data($params);
            return $this->resData('success', ['pageNum' => $re[3], 'page' => $re[2], 'count' => $re[1], 'data' => $re[0]]);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 重复房源
     * @return false|string
     */
    public function getRepeatHouse() {
        try {
            $params = $this->checkParams('lat,lng');
            if ($params === false) return $this->resErr();
            HouseModel::getRepeatHouse($params, $data, $count);
            return $this->resData('success', compact('count', 'data'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源文件
     * @return false|string
     */
    public function getHouseConfig(){
        try {
            $params = $this->checkParams ('page');
            $HouseModel = new HouseModel();
            $info=$this->uInfo;
            $params['company_id']=$info['company_id'];
            $pageSize=10;
            $data = $HouseModel->getHouseConfig($params,$pageSize);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }

    }

    /**
     * 获取房源开拍倒计时
     */
    public function countdown(){
        try {
            $params = $this->checkParams ('id');
            $HouseModel = new HouseModel();
            $data = $HouseModel->countdown($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源分享 海报
     * @return false|string
     */
    public function housePoster(){
        try {
            $params = $this->checkParams ('id');
            $info=$this->uInfo;
            $HouseModel = new HouseModel();
            $data = $HouseModel->housePoster($params,$info['company_id']);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 房源清单
     * @return false|string
     */
    public function houseDetailedList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $company_id=$this->uInfo['company_id'];
            $list = HouseModel::houseDetailedList($params,$company_id);
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源清单 城市分享
     * @return false|string
     */
    public function houseDetailedCityShare(){
        try {
            $params = $this->checkParams ('');
            $info=$this->uInfo;
            $HouseModel = new HouseModel();
            $data = $HouseModel->houseDetailedShare($params,$info['company_id']);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源清单 区域分享
     * @return false|string
     */
    public function houseDetailedAreaShare(){
        try {
            $params = $this->checkParams ('area_code');
            $info=$this->uInfo;
            $HouseModel = new HouseModel();
            $data = $HouseModel->houseDetailedShare($params,$info['company_id']);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
